<?php
/**
 * Ganglib
 *
 * LICENSE
 *
 * This source file is subject to the GNU Lesser Generak Public license.
 * 
 * It is available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/lgpl.html
 *
 * @category    Ganglib
 * @package    Div_Db_Distributor_Strategy_RSS
 * @copyright  Copyright (c) 2008 Ganglib project members (http://www.code.google.com/p/ganglib)
 * @license     http://www.gnu.org/licenses/lgpl.html
 */


/**
 * @see Zend_Feed_Rss
 */
require_once 'Zend/Feed/Rss.php';

 /** @see Div_Db_Distributor_Strategy_RSS_Abstract */
require_once('Div/Db/Distributor/Strategy/RSS/Abstract.php');


/**
 * RSS Horizontal strategy.
 * 
 * RSS strategy is an example - shows that strategies could operate on every data source, not
 * only on relational databases.
 * 
 * This strategy could operate on multiple RSS channels which could be choosen via FROM clause.
 * 
 * AND and LIKE operators are implemented in WHERE clause.
 * 
 * @package    Div_Db_Distributor_Strategy_RSS
 * @copyright  Copyright (c) 2008 Ganglib project members (http://www.code.google.com/p/ganglib)
 * @license     http://www.gnu.org/licenses/lgpl.html
 */
class Div_Db_Distributor_Strategy_RSS_Horizontal extends Div_Db_Distributor_Strategy_RSS_Abstract 
{
	/**
	 * avalibale fiels in rss feed
	 *
	 * @var array
	 */
	private static $fields = array(
		'title',
		'link',
		'description',
		//'pubDate',
		//'etc',	
	);
	
	public static function addField()
	{
		$arr = func_get_args();
		array_push(self::$fields,$arr);
		self::$fields = array_unique(self::$fields);
	}
	
	public static function getFields()
	{
		return self::$fields;
	}
	
	public static function clearFields()
	{
		 self::$fields = array();
		 return true;
	}
	
	
	
	/**
	 * Query to RSS
	 *
	 * @param Div_Db_Query $query
	 * @return array()
	 */
	public function distributeQuery(Div_Db_Query $query)
	{
		$modelArray = $query->toModelArray();

		$result = array();
		$tab = array();
		
		foreach ($modelArray['from'] as $from)
		{
			$feed = new Zend_Feed_Rss($from);			
			if($feed->valid())
			{
				foreach ($feed as $key => $item)
				{
					foreach (self::$fields as $field) 
					{
						if(is_string($field) && $item->$field() )
						{
							$tab[$field] = $item->$field();
						}
					}					
					$result[] =$tab;					
				}
				$tab = array();
			}
		}
		
		if(isset($modelArray['where']) && !empty($modelArray['where']) && $result)
		{
			$result_where = array();	
			
			foreach ($result as  $tab) 				
			{				
				foreach ($modelArray['where'] as $key => $where) 
				{					
					list($field, $value) = array($key, $where['value']);
					
					if( isset($tab[$field]) ) 
					{
						switch ($where['prefix']) 
						{
							case 'LIKE':
								if(stripos($tab[$field], $value)!== false)
									$result_where[] = $tab;
							break;
							
							case 'AND':
								if($tab[$field] == $value)
									$result_where[] = $tab;
							break;
							
							default:
								//var_dump( $where['prefix']).'..';
							break;
						}
					}
				}				
			}			
			return $result_where;
		}
		
		return $result;
	}
	
	/**
	 * Execute query
	 *
	 * @param Div_Db_Query $query
	 * @return array()
	 * @access public
	 */
	public function execute(Div_Db_Query $query)
	{
		$result = $this->distributeQuery($query);
		$this->isCacheable = false;
		
		if($this->queryIsSelect($query))
		{			
			$this->isCacheable = true;
		}
			
		return $result;
	}
}